<?php
namespace app\index\domain;

class TermList extends BaseList
{
    //后续连贯标签
    static protected $tags = [
        'nav'=>[],
        'content'=>[],
        'list'=>[],
    ];

    private $_params    = [];

    public function __construct($parentId)
    {
        $this->_parentId = $parentId;
        $this->initParam();
    }

    /**
     * 获取列表中某个内容的数据对象
     */
    protected function createObj($objParam)
    {
        //return new Term($objParam['id'], 'id');
        return Classes::get('Term', $objParam);
    }

    /**
     * 初始化参数组
     */
    public function initParam()
    {
        if(isset($this->_initParams)){
            $this->_params = $this->_initParams;
            return;
        }
        $this->_initParams = [];
        $this->_params = $this->_initParams;
        return;
    }

    public function valueGet($key)
    {
        if(isset($this->getList()[$key])){
            $index = $this->getList()[$key];
            return $this->createObj($index);
        }
        return null;
    }

    /**
     * 获取导航
     */
    public function getNav()
    {
        $this->_params['where']['display_nav']=1;
        return $this;
    }

    /**
     * 获取栏目下的内容
     */
    public function getContent()
    {
        if(isset($this->_content))
            return $this->_content;
        //if($content = new ContentList($this->_self['id']))
        $list = $this->getList();
        foreach($list as &$term){
            $term = $term['id'];
        };unset($term);
        if($content = Classes::get('ContentList', $list))
            return $this->_content = $content;
    }

    /**
     * 根据参数组获取列表
     */
    protected function getList()
    {
        $params = $this->_params;
        $cacheKey = md5('contentList'.serialize($params));
        if(\think\Config::get('auto_init_param')==1)
            $this->initParam();
        if(isset($this->_cache[$cacheKey]))
            return $this->_cache[$cacheKey];
        if($contentList = D('index/Term')->getTermChild($this->_parentId, $params)){
            return $this->_cache[$cacheKey] = $contentList;
        }
        return [];
    }
}

